package com.itnetwork.Action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import java.util.logging.LogManager;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

public class ValidationAction extends ActionSupport implements ServletRequestAware,ServletResponseAware
{
 /**
  * 获取验证码图片，发给前台，通过ajax
	 * 
	 */
	private static final long serialVersionUID = 1L;
private String validationString="23456789abcdefghkmnpqrstuvwxyzABCDEFGHKLMNPQRSTUVWXYZ";
	private HttpServletRequest request;
	//LogManager oo=(LogManager) LogManager.getLoggingMXBean();
	private HttpServletResponse response;
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}
	public void setServletResponse(HttpServletResponse response) {
		this.response=response;
	}

	private Color getRandomColor(int minColor, int maxColor)
	{
		Random random = new Random();
		// 保存minColor最大不会超过255
		if (minColor > 255)
			minColor = 255;
		//  保存minColor最大不会超过255
		if (maxColor > 255)
			maxColor = 255;
		//  获得红色的随机颜色值
		int red = minColor + random.nextInt(maxColor - minColor);
		//  获得绿色的随机颜色值
		int green = minColor + random.nextInt(maxColor - minColor);
		//  获得蓝色的随机颜色值
		int blue = minColor + random.nextInt(maxColor - minColor);
		return new Color(red, green, blue);
	}
	
	
	@Override
	public String execute() throws IOException
	{
		StringBuilder newBuilder = new StringBuilder();
		Random random=new Random();
		String[] fontNames = { "Times New Roman", "Book antiqua",  "Arial" };
		int length=validationString.length();
		response.setHeader("ragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		
		int width=100;
		int height=30;
		
		BufferedImage imag =new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		Graphics g =imag.getGraphics();
		g.setColor(getRandomColor(192,244));
		g.fillRect(0, 0, width, height);
		g.setFont(new Font("Times New Roman", Font.ITALIC, height));
		g.setColor(getRandomColor(120, 180));
		for(int i=1;i<=random.nextInt(3)+3;i++)
		{
			char codeChar = validationString.charAt(random.nextInt(length));
			newBuilder.append(codeChar);
			g.setFont(new Font(fontNames[random.nextInt(3)],Font.ITALIC, height));
			g.setColor(getRandomColor(144,178));
			g.drawString(String.valueOf(codeChar), 16*i+random.nextInt(8), height-random.nextInt(10));
			
		}
		HttpSession session=request.getSession();
		session.setMaxInactiveInterval(5*60);
		//session.setAttribute("validationcode", newBuilder.toString());
		session.setAttribute("random",newBuilder.toString());
		g.dispose();
			OutputStream out=response.getOutputStream();
			ImageIO.write(imag, "JPEG", out);
			/*out.flush();*/
	
		
		return null;
	}

}
